package ar.com.candoit.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import ar.com.candoit.beans.Adress;
import ar.com.candoit.beans.Student;
import ar.com.candoit.dao.AdressEditor;
import ar.com.candoit.service.AdressService;
import ar.com.candoit.service.StudentService;

@Controller
@RequestMapping("/students")
public class StudentController {

	@Autowired
	private StudentService studentService;

	@Autowired
	private AdressService adressService;

	@InitBinder
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) {
		binder.registerCustomEditor(Adress.class, new AdressEditor(
				this.adressService));
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public ModelAndView saveStudent(@ModelAttribute("student") Student student,
			BindingResult result) {
		studentService.addStudent(student);
		return new ModelAndView("redirect:/students.html");
	}

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView listStudents() {
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("students", studentService.listStudent());
		return new ModelAndView("studentList", model);
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public ModelAndView addStudent(@ModelAttribute("student") Student student,
			BindingResult result) {

		List<Adress> lista = new ArrayList<Adress>();
		for (Adress ad : adressService.listAdress()) {
			lista.add(ad);
		}

		ModelAndView mav = new ModelAndView("addStudent");
		mav.addObject("lista", lista);
		return mav;
	}

	// delete
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public ModelAndView deleteStudent(
			@ModelAttribute("student") Student student, BindingResult result) {
		student = studentService.findStudentById(student.getStudentId());
		studentService.deleteStudent(student);
		return new ModelAndView("redirect:/students.html");
	}

	// update
	@RequestMapping(value = "/updateLoad", method = RequestMethod.POST)
	public ModelAndView updateLoadStudent(
			@ModelAttribute("student") Student student, BindingResult result) {

		// cargar Estudiante
		student = studentService.findStudentById(student.getStudentId());
		ModelAndView mav = new ModelAndView("updateStudent");
		mav.addObject("student", student);

		// cargar localidades
		List<Adress> lista = new ArrayList<Adress>();
		for (Adress ad : adressService.listAdress()) {
			lista.add(ad);
		}

		mav.addObject("lista", lista);

		return mav;
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ModelAndView updateStudent(
			@ModelAttribute("student") Student student, BindingResult result) {
		studentService.updateStudent(student);
		return new ModelAndView("redirect:/students.html");
	}

}
